package main.pmc;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpPmcPmid {
    @SneakyThrows
    public static void main(String[] args) {
        // 更新真是pmid 数据 原来解析的有错误，但是下载的路径文件夹还是错误的，所以匹配也是使用错误的pmid 到时候入库看下有没有真实的 有则使用真实的

        Db useTaiyi = Db.use();
        String saveTable = "pmcdown_new2";
        String from = "pmcdownziphtml";
        List<Entity> query = useTaiyi.query("select pmid,real_pmid from " + from);
        Map<Integer, Integer> pmidAndReal = new HashMap<>();
        for (Entity entity : query) {
            Integer pmid = entity.getInt("pmid");
            Integer realPmid = entity.getInt("real_pmid");
            pmidAndReal.put(pmid, realPmid);
        }
        pmidAndReal.forEach((k, v) -> {
            try {
                Entity entity = useTaiyi.queryOne("select pmid,real_pmid from  " + saveTable + " where pmid = " + k);
                if (entity != null) {
                    entity.set("real_pmid", v);
                    entity.setTableName(saveTable);
                    try {
                        useTaiyi.insertOrUpdate(entity, "pmid");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("有误删除 pmid = " + k + " real_pmid = " + v);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
